package com.WOWTradeNews.DataBase;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import com.WOWTradeNews.Utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class DBManager {
	private DBHelper dbHelper;
	private SQLiteDatabase db;
	private Context mContext;
	
	public DBManager(Context context, String dbName) {
		dbHelper = new DBHelper(context, dbName);
		db = dbHelper.getWritableDatabase();
		mContext = context;
	}
	
	
	
	public void addNews(News news) {
		ContentValues content = new ContentValues();
		//iid subject contents sid datein down_date author intPublish country city news_link keywords
		//date_build is_post img1
		content.put("iid", news.getIid());
		content.put("subject", news.getSubject());
		content.put("contents", news.getContents());
		content.put("sid", news.getSid());
		content.put("datein", news.getDatein());
		content.put("down_date", news.getDown_date());
		content.put("author", news.getAuthor());
		content.put("intPublish", news.getIntPublish());
		content.put("country", news.getCountry());
		content.put("city", news.getCity());
		content.put("news_link", news.getNews_link());
		content.put("keywords", news.getKeyWords());
		content.put("date_build", news.getDate_build());
		content.put("is_post", news.isIs_post() ? 1 : 0);
		content.put("img1", news.getImg1Url());
		content.put("txt1", news.getTxt1Url());
		content.put("newsType", news.getNewsType());
		content.put("acttime", news.getActtime());
		content.put("active", news.getActive());
		content.put("img2", news.getImg2Url());
		content.put("img3", news.getImg3Url());
		
		Bitmap bmp = news.getImg();
		if(bmp != null) {
			final ByteArrayOutputStream os = new ByteArrayOutputStream();
			bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
			content.put("imgData", os.toByteArray());
		}
		
		
		db.insert("news", null, content);
	}
	
	public void addNews(List<News> list) {
		if(list == null) return ;
		for(News news : list) {
			addNews(news);
		}
	}
	
	public List<News> getNewsList(int type) {
		List<News> list = new ArrayList<News>();
		Cursor c = db.rawQuery("SELECT * FROM news where newsType=?", new String[]{String.valueOf(type)});
		
		while(c.moveToNext()) {
			News news = new News();
			//iid subject contents sid datein down_date author intPublish country city news_link keywords
			//date_build is_post img1
			news.setId(c.getInt(c.getColumnIndex("_id")));
			news.setIid(c.getString(c.getColumnIndex("iid")));
			news.setSubject(c.getString(c.getColumnIndex("subject")));
			news.setContents(c.getString(c.getColumnIndex("contents")));
			news.setSid(c.getString(c.getColumnIndex("sid")));
			news.setDatein(c.getString(c.getColumnIndex("datein")));
			news.setDown_date(c.getString(c.getColumnIndex("down_date")));
			news.setAuthor(c.getString(c.getColumnIndex("author")));
			news.setIntPublish(c.getString(c.getColumnIndex("intPublish")));
			news.setCountry(c.getString(c.getColumnIndex("country")));
			news.setCity(c.getString(c.getColumnIndex("city")));
			news.setNews_link(c.getString(c.getColumnIndex("news_link")));
			news.setKeyWords(c.getString(c.getColumnIndex("keywords")));
			news.setDate_build(c.getString(c.getColumnIndex("date_build")));
			news.setIs_post(c.getInt(c.getColumnIndex("is_post")) == 1);
			news.setImg1Url(c.getString(c.getColumnIndex("img1")));
			news.setTxt1Url(c.getString(c.getColumnIndex("txt1")));
			news.setNewsType(c.getInt(c.getColumnIndex("newsType")));
			news.setActtime(c.getString(c.getColumnIndex("acttime")));
			news.setActive(c.getString(c.getColumnIndex("active")));
			news.setImg2Url(c.getString(c.getColumnIndex("img2")));
			news.setImg3Url(c.getString(c.getColumnIndex("img3")));
			byte[] in = c.getBlob(c.getColumnIndex("imgData"));
			if(in != null) {
				Bitmap bmp = BitmapFactory.decodeByteArray(in, 0, in.length);
				news.setImg(bmp);
			}
			
			
			list.add(news);
		}
		c.close();
		
		return list;
	}
	
	public List<News> getNewsByCountry(String country) {
		List<News> list = new ArrayList<News>();
		Cursor c = db.rawQuery("SELECT * FROM news WHERE country=?", new String[]{country});
		while(c.moveToNext()) {
			News news = new News();
			//iid subject contents sid datein down_date author intPublish country city news_link keywords
			//date_build is_post img1
			news.setIid(c.getString(c.getColumnIndex("iid")));
			news.setSubject(c.getString(c.getColumnIndex("subject")));
			news.setContents(c.getString(c.getColumnIndex("contents")));
			news.setSid(c.getString(c.getColumnIndex("sid")));
			news.setDatein(c.getString(c.getColumnIndex("datein")));
			news.setDown_date(c.getString(c.getColumnIndex("down_date")));
			news.setAuthor(c.getString(c.getColumnIndex("author")));
			news.setIntPublish(c.getString(c.getColumnIndex("intPublish")));
			news.setCountry(c.getString(c.getColumnIndex("country")));
			news.setCity(c.getString(c.getColumnIndex("city")));
			news.setNews_link(c.getString(c.getColumnIndex("news_link")));
			news.setKeyWords(c.getString(c.getColumnIndex("keywords")));
			news.setDate_build(c.getString(c.getColumnIndex("date_build")));
			news.setIs_post(c.getInt(c.getColumnIndex("is_post")) == 1);
			news.setImg1Url(c.getString(c.getColumnIndex("img1")));
			news.setTxt1Url(c.getString(c.getColumnIndex("txt1")));
			news.setNewsType(c.getInt(c.getColumnIndex("newsType")));
			
			byte[] in = c.getBlob(c.getColumnIndex("imgData"));
			if(in != null) {
				Bitmap bmp = BitmapFactory.decodeByteArray(in, 0, in.length);
				news.setImg(bmp);
			}
			
			list.add(news);
		}
		return list;
	}
	
	public void delateAllNews() {
		db.delete("news", null, null);
	}
	
	public void deleteTypeNews(int type) {
		db.delete("news", "newsType=?", new String[]{String.valueOf(type)});
	}
	
	public News findByIid(String iid) {
		News news = new News();
		Cursor c = db.rawQuery("SELECT * FROM news WHERE iid=? AND newsType!=10", new String[]{iid});
		if(c.getCount() == 0) return null;
		if(c.moveToFirst()) {
			news.setIid(c.getString(c.getColumnIndex("iid")));
			news.setSubject(c.getString(c.getColumnIndex("subject")));
			news.setContents(c.getString(c.getColumnIndex("contents")));
			news.setSid(c.getString(c.getColumnIndex("sid")));
			news.setDatein(c.getString(c.getColumnIndex("datein")));
			news.setDown_date(c.getString(c.getColumnIndex("down_date")));
			news.setAuthor(c.getString(c.getColumnIndex("author")));
			news.setIntPublish(c.getString(c.getColumnIndex("intPublish")));
			news.setCountry(c.getString(c.getColumnIndex("country")));
			news.setCity(c.getString(c.getColumnIndex("city")));
			news.setNews_link(c.getString(c.getColumnIndex("news_link")));
			news.setKeyWords(c.getString(c.getColumnIndex("keywords")));
			news.setDate_build(c.getString(c.getColumnIndex("date_build")));
			news.setIs_post(c.getInt(c.getColumnIndex("is_post")) == 1);
			news.setImg1Url(c.getString(c.getColumnIndex("img1")));
			news.setTxt1Url(c.getString(c.getColumnIndex("txt1")));
			news.setNewsType(c.getInt(c.getColumnIndex("newsType")));
			
			byte[] in = c.getBlob(c.getColumnIndex("imgData"));
			if(in != null) {
				Bitmap bmp = BitmapFactory.decodeByteArray(in, 0, in.length);
				news.setImg(bmp);
			}
			
		}
		
		c.close();
		
		return news;
	}
	
	public void deleteByIid(String iid) {
		Log.d("zhaosdfa", "iid----->>>>" + iid);
		//db.execSQL("DELETE news WHERE iid=?", new String[]{iid});
		db.delete("news", "iid=?", new String[]{iid});
	}
	
	public void deleteByType(int type) {
		db.delete("news", "newsType=?", new String[]{String.valueOf(type)});
	}
	
	public void deleteOthers(TreeSet<String> set) {
		Cursor c = db.rawQuery("SELECT * FROM news", null);
		while(c.moveToNext()) {
			String iid = c.getString(c.getColumnIndex("iid"));
			if(!set.contains(iid)) {
				deleteByIid(iid);
			}
		}
	}
	
	public void deleteTypeOthers(TreeSet<String> set, int type) {
		Cursor c = db.rawQuery("SELECT * FROM news where newsType=?", new String[]{String.valueOf(type)});
		while(c.moveToNext()) {
			String iid = c.getString(c.getColumnIndex("iid"));
			if(!set.contains(iid)) {
				deleteByIid(iid);
			}
		}
	}
	
	public void closeDB() {
		dbHelper.close();
		db.close();
	}
	
}






